home *** CD-ROM | disk | FTP | other *** search
/ Delphi 2.0 - Programmer's Utilities Power Pack / Delphi 2.0 Programmer's Utilities Power Pack.iso / s_to_z / tpack / resizer.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-09-15  |  8.0 KB  |  292 lines

  1. unit Resizer;
  2.  
  3. { The ReSizer is a work in progress.
  4.   Ulimately I will get it to work the way I want to but for the moment is rests with
  5.   some issues unresolved. If you register, You will receive a version that I'm happy
  6.   with as soon as I get back to this. (Do you wish to spend all your time on interfaces?)}
  7.  
  8.  
  9. interface
  10.  
  11. uses Forms, SysUtils, WinProcs,
  12.   Classes, Controls, Buttons, StdCtrls, ExtCtrls
  13. , Toolbar;
  14.  
  15. type
  16.   TReSizerPanelControl = class(TControl);
  17.   TReSizerPanel = class(TExtendedPanel)
  18.   private
  19.     fAdjoined: TControl;
  20.     fBoundControl: TControl;
  21.     fFormDragOver: TDragOverEvent;
  22.     fControlDragOver: TDragOverEvent;
  23.     function GetAlign:TAlign; {override;}
  24.     procedure SetAlign(Value:TAlign); {override; }
  25.   protected
  26.     procedure SetBoundControl(Value:TControl);
  27.     procedure SetAdjoined(Value:TControl);
  28.     function StoreBoundControl:Boolean;
  29.     procedure LinkDragDrop;
  30.     procedure UnLinkDragDrop;
  31.     procedure PositionViaForm(x,y:Integer);
  32.     procedure PositionViaControl(x,y:Integer);
  33.     procedure PositionViaAdjoined(x,y:Integer);
  34.     procedure AlignSelf;
  35.   public
  36.     constructor Create(aOwner:TComponent); Override;
  37.     constructor CreateFor(aOwner:TComponent;aControl:TControl);
  38.     procedure DragOver(Sender, Source: TObject; X, Y: Integer; State: TDragState; var Accept: Boolean);
  39.     destructor Destroy; Override;
  40.     procedure Notification(AComponent: TComponent; Operation: TOperation); Override;
  41.   published
  42.     property Align: TAlign read GetAlign write SetAlign;
  43.     property Adjoined: TControl read fAdjoined write SetAdjoined;
  44.     property BoundControl: TControl read fBoundControl write SetBoundControl Stored StoreBoundControl;
  45.     end;
  46.  
  47. {------------------------------------------------------------------------------}
  48.  
  49. implementation
  50.  
  51. {------------------------------------------------------------------------------}
  52.  
  53. constructor TReSizerPanel.CreateFor(aOwner:TComponent;aControl:TControl);
  54. {create a ResizerPanel from another control's creator using this proc. function actually.}
  55. begin
  56.   Create(aOwner); {call official constructor shown below/next}
  57.   Parent:=TForm(aOwner); {this may blow up of course.}
  58.   BoundControl:=TReSizerPanelControl(aControl); {note the typecasting!}
  59.   UniqueName:='ReSizerPanel'; {see the panel code. setting the name now }
  60. end;
  61.  
  62. constructor TReSizerPanel.Create(aOwner:TComponent);
  63. begin
  64.   inherited Create(aOwner);
  65.   Hint:='Drag to resize the area';
  66.   Caption:='';
  67.   Width := 3;
  68.   Align := alNone;
  69.   DragMode := dmAutomatic;
  70.   BevelInner := bvRaised;
  71. end;
  72.  
  73. destructor TReSizerPanel.Destroy;
  74. begin
  75.   UnLinkDragDrop;
  76.   inherited Destroy;
  77. end;
  78.  
  79. procedure TReSizerPanel.Notification(AComponent: TComponent; Operation: TOperation);
  80. begin
  81.   inherited Notification(AComponent, Operation);
  82.   if Operation = opRemove then begin
  83.     if fAdjoined=aComponent then
  84.       fAdjoined:=nil;
  85.     if fBoundControl=aComponent then
  86.       fBoundControl:=nil;
  87.     end;
  88. end;
  89.  
  90. procedure TReSizerPanel.SetAdjoined(Value:TControl);
  91. begin
  92.   if Value=nil then
  93.     Value:=TForm(Owner);
  94.   if fAdjoined<>Value then begin
  95.     UnLinkDragDrop;
  96.     fAdjoined:=Value;
  97.     LinkDragDrop;
  98.     end;
  99. end;
  100.  
  101.  
  102. procedure TReSizerPanel.SetBoundControl(Value:TControl);
  103. begin
  104.   if fBoundControl<>Value then begin
  105.     UnLinkDragDrop;
  106.     fBoundControl:=Value;
  107.     LinkDragDrop;
  108.     Align:=fBoundControl.Align;
  109.     AlignSelf;
  110.     end;
  111. end;
  112.  
  113.  
  114. procedure TReSizerPanel.PositionViaAdjoined(x,y:Integer);
  115. begin
  116.   case Align of
  117.     alLeft:   fBoundControl.Width:=  fBoundControl.Width+x;
  118.     alRight:  fBoundControl.Width:=  fBoundControl.Width+fAdjoined.Width-x-1-self.Width;
  119.     alTop:    fBoundControl.Height:= fBoundControl.Height+y;
  120.     alBottom: fBoundControl.Height:= fBoundControl.Height+fAdjoined.Height-y-self.Height;
  121.     end;
  122. end;
  123.  
  124.  
  125. procedure TReSizerPanel.PositionViaForm(x,y:Integer);
  126. const
  127.   i=2;
  128. begin
  129.   case Align of
  130.     alBottom:
  131.       if y+self.Height+i>=tForm(Owner).ClientHeight then
  132.         y:=tForm(Owner).ClientHeight-self.Height-i;
  133.     end;
  134.   case Align of
  135.     alLeft:   fBoundControl.Width:=  x-fBoundControl.Left;
  136.     alRight:  fBoundControl.Width:=  fBoundControl.Width+fBoundControl.Left-x-self.Width;
  137.     alTop:    fBoundControl.Height:= y-fBoundControl.Top;
  138.     alBottom: fBoundControl.Height:=fBoundControl.Height+fBoundControl.Top-y-self.Height;
  139.     end;
  140. end;
  141.  
  142.  
  143. procedure TReSizerPanel.PositionViaControl(x,y:Integer);
  144. const
  145.   i=2;
  146. begin
  147.   case Align of
  148.     alRight:
  149.       if x+i>=fBoundControl.Width then
  150.         x:=fBoundControl.Width-i;
  151.     alBottom:
  152.       if y+i>=fBoundControl.Height then
  153.         y:=fBoundControl.Height-i;
  154.     end;
  155.   case Align of
  156.     alLeft:   fBoundControl.Width:=  x;
  157.     alRight:  fBoundControl.Width:=  fBoundControl.Width-x;
  158.     alTop:    fBoundControl.Height:= y;
  159.     alBottom: fBoundControl.Height:= fBoundControl.Height-y;
  160.     end;
  161. end;
  162.  
  163.  
  164. procedure TReSizerPanel.AlignSelf;
  165. begin
  166.   case Align of
  167.     alLeft:   Left:= fBoundControl.Left+fBoundControl.Width;
  168.     alRight:  Left:= fBoundControl.Left-Width;
  169.     alTop:    Top:=  fBoundControl.Top+fBoundControl.Height;
  170.     alBottom: Top:=  fBoundControl.Top-Height;
  171.     end;
  172. end;
  173.  
  174.  
  175. function TReSizerPanel.StoreBoundControl:Boolean;
  176. begin
  177. {  UnLinkDragDrop;}
  178.   Result:=True;
  179. end;
  180.  
  181. function TReSizerPanel.GetAlign:TAlign;
  182. begin
  183.   result:=TExtendedPanel(Self).Align;
  184. end;
  185.  
  186.  
  187. procedure TReSizerPanel.SetAlign(Value:TAlign);
  188. var
  189.   c:TCursor;
  190.   b:TPanelBevel;
  191. begin
  192.   if fBoundControl<>nil then
  193.     fBoundControl.Align:=Value;
  194.   TExtendedPanel(Self).Align:=Value;
  195.   if fBoundControl<>nil then
  196.     AlignSelf;
  197.   {}
  198.   case Value of
  199.   alLeft,
  200.   alRight:  Width := 3;
  201.   alTop,
  202.   alBottom: Height := 3;
  203.     end;
  204.   {}
  205.   case Value of
  206.   alLeft,
  207.   alRight:  c:= crHSplit;
  208.   alTop,
  209.   alBottom: c:= crVSplit;
  210.   else      c:= crDrag;
  211.     end;
  212.   Cursor := c;
  213.   DragCursor := c;
  214.   {}
  215.   case Value of
  216.   alBottom,
  217.   alRight:  b:= bvRaised;
  218.   alTop,
  219.   alLeft:   b:= bvLowered;
  220.   else      b:= bvLowered;
  221.     end;
  222.   if b=bvRaised then b:=bvLowered else b:=bvRaised;
  223.   BevelOuter:=b;
  224.   BevelInner:=b;
  225.   {}
  226. end;
  227.  
  228.  
  229. procedure TReSizerPanel.UnLinkDragDrop;
  230. begin {by casting we can access the private parts}
  231.   if (fBoundControl<>nil) and assigned(fControlDragOver) then
  232.     TReSizerPanelControl(fBoundControl).OnDragOver:=fControlDragOver;
  233.   if (fAdjoined<>nil) and assigned(fFormDragOver) then
  234.     TReSizerPanelControl(fAdjoined).OnDragOver:=fFormDragOver;
  235. end;
  236.  
  237.  
  238. procedure TReSizerPanel.LinkDragDrop;
  239. begin
  240.   if fBoundControl<>nil then
  241.     with TReSizerPanelControl(fBoundControl) do begin
  242.       fControlDragOver:=OnDragOver;
  243.       OnDragOver:=DragOver;
  244.       end;
  245.   if fAdjoined<>nil then
  246.     with TReSizerPanelControl(fAdjoined) do begin
  247.       fFormDragOver:=OnDragOver;
  248.       OnDragOver:=DragOver;
  249.       end;
  250. end;
  251.  
  252.  
  253. procedure TReSizerPanel.DragOver(Sender, Source: TObject; X, Y: Integer; State: TDragState; var Accept: Boolean);
  254. const
  255.   i=2;
  256. begin
  257. {  tlabel(tform(owner).findcomponent('Label1')).Caption:=
  258.     'Self: '+name+'/'+fBoundControl.ClassName+'.'+fBoundControl.Name;
  259.   tlabel(tform(owner).findcomponent('Label2')).Caption:=
  260.     'Sender: '+sender.classname+'.'+tcomponent(sender).name+'  '
  261.    +'Source: '+source.classname+'.'+tcomponent(source).name+'  ';   {}
  262.  
  263.   if Self=TReSizerPanel(Source) then begin
  264.     if y<i then
  265.       y:=i;
  266.     if x<i then
  267.       x:=i;
  268.     if Sender = TObject(fAdjoined) then
  269.       if fAdjoined=Owner then
  270.         PositionViaForm(x,y)
  271.       else
  272.         PositionViaAdjoined(x,y)
  273.     else
  274.       PositionViaControl(x,y);
  275.     AlignSelf;
  276.     end;
  277. {  else}
  278.     if Sender = TObject(fAdjoined) then
  279.       if assigned(fFormDragOver) then
  280.         fFormDragOver(fAdjoined,Source,X,Y,State,Accept)
  281.       else
  282.     else
  283.       if Sender = TObject(fBoundControl) then
  284.         if assigned(fControlDragOver) then
  285.           fControlDragOver(Sender,Source,X,Y,State,Accept); {}
  286. end;
  287.  
  288. {------------------------------------------------------------------------------}
  289.  
  290. end.
  291.  
  292.